package com.klikmee.sms.chat.web.filters;

import java.text.MessageFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.klikmee.sms.cef.CoreAdapter;
import com.klikmee.sms.cef.services.Service;
import com.klikmee.sms.chat.web.dao.AnswerDAO;
import com.klikmee.sms.chat.web.dao.BulkGroupItemDAO;
import com.klikmee.sms.chat.web.dao.QuestionDAO;
import com.klikmee.sms.services.chat.model.Answer;
import com.klikmee.sms.services.chat.model.BulkGroupItem;
import com.klikmee.sms.services.chat.model.Question;

/**
 * Message filter that is activated on user unsubscribe request and
 * is used to unsubscribe user from receiving bulk messages sent from
 * chat service.
 *  
 * @author makcro
 *
 */
public class UnsubscribeFilter implements MessageFilter {

	/**
	 * Logger instance
	 */
	private Logger logger = Logger.getLogger(UnsubscribeFilter.class);
	
	/**
	 * Reference to the bulk group item dao, used to update subscribers status
	 */
	private BulkGroupItemDAO bulkGroupItemDAO;
	
	/**
	 * Regex that activates this filter
	 */
	private String activationExpresion;

	/**
	 * Instance of {@link CoreAdapter} used to send info messages after unsubscription is complete.
	 */
	private CoreAdapter coreAdapter;

	/**
	 * Text of the info message to be sent to the user
	 */
	private String unSubscribeInfoText;

	/**
	 * Core id of the service used to unsubscribe user
	 */
	private Long unSubscribeServiceId;

	/**
	 * Core id of the charging used to unsubscribe the user
	 */
	private Long unSubscribeChargingId;

	/**
	 * Dao components used to modify question/answer
	 */
	private AnswerDAO answerDAO;

	private QuestionDAO questionDAO;
	
	
	/**
	 * Tests question for activation expression and if matched tries to unsubscribe user with the given id,
	 * by updating {@link BulkGroupItem#setDisabled(boolean)} property.
	 * 
	 */
	@Override
	public boolean processMessage(Question question) {

		if(question.getQuestion().matches(activationExpresion)){
			logger.info(MessageFormat.format("Successfull match on Unsubscribe filter, message id = [{0}]",question.getCoreID()));

			BulkGroupItem bulkGroupItem = this.bulkGroupItemDAO.findByPhoneNumber(question.getFromNumber());
			if(bulkGroupItem != null){
				bulkGroupItem.setDisabled(true);
				this.bulkGroupItemDAO.edit(bulkGroupItem);
				
				logger.info(MessageFormat.format("Successfuly unsubscribed user with phonenumber [{0}]", question.getFromNumber()));
			}
			
			else {
				logger.info(MessageFormat.format("Could not find subsciber with phonenumber = [{0}], ignoring...", question.getFromNumber()));
			}
			// prepare answer
			Answer answer = new Answer();
			answer.setAnswer(this.unSubscribeInfoText);
			answer.setDateAnswered(new Date());
			answer.setFromNumber(question.getToNumber());
			answer.setToNumber(question.getFromNumber());
			answer.setVisable(false);
			answer.setQuestion(question);
			// send it
			Long infoCoreId = this.coreAdapter.sendMessage(this.unSubscribeChargingId, this.unSubscribeServiceId, Service.SERVICE_FLAG_SEMANTIC_OK, question.getCoreID(), this.unSubscribeInfoText);
			// and save
			answer.setCoreID(infoCoreId);
			answerDAO.create(answer);
			
			// update question so it's not visible to operaters
			question.setVisable(false);
			questionDAO.edit(question);
			
			return true;
		}
		return false;
	}

	public void setActivationExpresion(String activationExpresion) {
		this.activationExpresion = activationExpresion;
	}

	public String getActivationExpresion() {
		return activationExpresion;
	}

	public void setBulkGroupItemDAO(BulkGroupItemDAO bulkGroupItemDAO) {
		this.bulkGroupItemDAO = bulkGroupItemDAO;
	}
	
	public void setCoreAdapter(CoreAdapter coreAdapter) {
		this.coreAdapter = coreAdapter;
	}

	public void setUnSubscribeInfoText(String unSubscribeInfoText) {
		this.unSubscribeInfoText = unSubscribeInfoText;
	}

	public void setUnSubscribeServiceId(Long unSubscribeServiceId) {
		this.unSubscribeServiceId = unSubscribeServiceId;
	}

	public void setUnSubscribeChargingId(Long unSubscribeChargingId) {
		this.unSubscribeChargingId = unSubscribeChargingId;
	}

	public void setAnswerDAO(AnswerDAO answerDAO) {
		this.answerDAO = answerDAO;
	}

	public void setQuestionDAO(QuestionDAO questionDAO) {
		this.questionDAO = questionDAO;
	}
	
}
